import {
	requestURL
} from './config'

import  { setToken, getToken } from './auth.js';
import { tokenRefresh } from '@/common/api/quickRegister/quickRegister.js';
	
function Request(data) {
	const input = data.data
	return new Promise((resolve, reject) => {
	// console.log(data)
	// console.log("method:"+data.method)
	// console.log(data.data)   
	// console.log("url:"+data.url)
		if(uni.getStorageSync('isAlreadyLogin')== false||uni.getStorageSync('isAlreadyLogin')==''||uni.getStorageSync('isAlreadyLogin')==null){
			uni.showToast({
				title: "您暂未登录",
				icon: 'none',
				mask: true,
				duration: 1000
			});
		}else {
			uni.request({
				url: requestURL + data.url,
				data: input,  
				method: data.method,
				header: {
						'content-type': 'application/json',
						'Authorization': getToken() || ''
				},
				success: (res => {
				if (res.statusCode === 200) {
						console.log("成功200")
						resolve(res)
				}else if(res.statusCode === 401){
					uni.showToast({
						title: "token凭证已过期，请重新登录",
						icon: 'none',
						mask: true,
						duration: 1000
					});
					tokenRefresh().then(res=>{
						console.log(res)
						setToken(res.data.data.tokenHead + ' ' + res.data.data.token);
					})
					console.log(getToken())
				} else {
						console.log("失败1")
						reject(res)
				}
			  }),
			  fail: (res => {
					  console.log("失败2")
				reject(res)
			  })
			})
		}
    })
}

export default function(data, url, method) {
	return Request(data, url, method)
}
